package com.ruoyi.web.controller.address;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.TbAddress;
import com.ruoyi.web.service.ITbAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户端地址接口
 */
@RestController
@RequestMapping("/userAddress")
public class AddressUserController extends BaseController {
    @Autowired
    private ITbAddressService tbAddressService;

    /**
     * 查询用户地址溥列表
     */
    @GetMapping("/selectAll")
    public AjaxResult list() {
        List<TbAddress> list = tbAddressService.selectTbAddressList(new TbAddress().setUserId(getUserId()));
        return AjaxResult.success(list);
    }

    /**
     * 修改默认地址
     *
     * @return
     */
    @PutMapping("/updateDef/{id}")
    public AjaxResult updateDef(@PathVariable("id") Long id) {
        return toAjax(tbAddressService.updateDef(getUserId(), id));
    }

    /**
     * 获取默认地址
     */
    @GetMapping("/getDef")
    public AjaxResult getDef() {
        TbAddress address = tbAddressService.selectDef(getUserId());
        return success(address);
    }

    /**
     * 获取用户地址溥详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbAddressService.selectTbAddressById(id));
    }

    /**
     * 新增用户地址溥
     */
    @Log(title = "用户地址溥", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAddress tbAddress) {
        tbAddress.setUserId(getUserId());
        return toAjax(tbAddressService.insertTbAddress(tbAddress));
    }

    /**
     * 修改用户地址溥
     */
    @Log(title = "用户地址溥", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAddress tbAddress) {
        tbAddress.setUserId(getUserId());
        return toAjax(tbAddressService.updateTbAddress(tbAddress));
    }

    /**
     * 删除用户地址溥
     */
    @Log(title = "用户地址溥", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbAddressService.deleteTbAddressByIds(ids));
    }
}
